package com.yuuzy.view;

import com.yuuzy.dao.WipDAO;
import com.yuuzy.model.Wip;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

public class ManageWip extends JInternalFrame {
    private JTable wip_table;
    private JTextField QW_nameText;
    private JTextField wip_idText;
    private JTextField wip_nameText;
    private JTextArea wip_descriptionText;
    private JTextField wip_amountText;
    private JTextField wip_yearText;
    private JRadioButton wip_upRB;
    private JRadioButton wip_downRB;

    private WipDAO wipDAO = new WipDAO();
    private ButtonGroup group = new ButtonGroup();

    /**
     * Create the frame.
     */
    public ManageWip() {
        setTitle("理财产品管理");
        setClosable(true);
        setIconifiable(true);
        setBounds(100, 100, 1000, 1100);

        JScrollPane scrollPane = new JScrollPane();

        JLabel label = new JLabel("基金名称：");
        label.setFont(new Font("宋体", Font.PLAIN, 20));

        QW_nameText = new JTextField();
        QW_nameText.setFont(new Font("宋体", Font.PLAIN, 20));
        QW_nameText.setColumns(10);

        JButton button = new JButton("查询 ");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        button.setIcon(new ImageIcon(ManageWip.class.getResource("/image/pm-query-16.png")));
        button.setFont(new Font("宋体", Font.PLAIN, 20));

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "\u4FEE\u6539/\u5220\u9664\u7406\u8D22\u4EA7\u54C1", TitledBorder.LEADING, TitledBorder.TOP, null, SystemColor.textHighlight));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(196)
                                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                                                .addGap(27)
                                                .addComponent(QW_nameText, GroupLayout.PREFERRED_SIZE, 264, GroupLayout.PREFERRED_SIZE)
                                                .addGap(65)
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(102)
                                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 765, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(101)
                                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 771, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(112, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addContainerGap(85, Short.MAX_VALUE)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(70))
                                                        .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                                                .addComponent(QW_nameText, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(66))
                                                        .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(64)))
                                                .addGap(277))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(157)
                                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 248, GroupLayout.PREFERRED_SIZE)
                                                .addGap(54)))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(panel, GroupLayout.PREFERRED_SIZE, 540, GroupLayout.PREFERRED_SIZE)
                                .addGap(60))
        );

        JLabel lblNewLabel = new JLabel("理财产品编号：");

        wip_idText = new JTextField();
        wip_idText.setEditable(false);
        wip_idText.setText("");
        wip_idText.setColumns(10);

        JLabel label_1 = new JLabel("理财产品名称：");

        wip_nameText = new JTextField();
        wip_nameText.setColumns(10);

        JLabel label_2 = new JLabel("理财产品价格：");

        wip_amountText = new JTextField();
        wip_amountText.setColumns(10);

        JLabel label_3 = new JLabel("理财产品年限：");

        wip_yearText = new JTextField();
        wip_yearText.setColumns(10);

        JLabel label_4 = new JLabel("是否上线：");

        wip_upRB = new JRadioButton("是");
        group.add(wip_upRB);
        wip_upRB.setSelected(true);

        wip_downRB = new JRadioButton("否");
        group.add(wip_downRB);

        JLabel label_5 = new JLabel("理财产品介绍：");

        wip_descriptionText = new JTextArea();

        JButton btnNewButton = new JButton("修改 ");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateActionPerformed(e);
            }
        });
        btnNewButton.setIcon(new ImageIcon(ManageWip.class.getResource("/image/icon_update.png")));

        JButton btnNewButton_1 = new JButton("删除 ");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteActionPerformed(e);
            }
        });
        btnNewButton_1.setIcon(new ImageIcon(ManageWip.class.getResource("/image/delete.png")));
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(34)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel)
                                                        .addComponent(label_1, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE))
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addGap(9)
                                                                .addComponent(wip_nameText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(32)
                                                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
                                                                .addGap(7)
                                                                .addComponent(wip_amountText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(gl_panel.createSequentialGroup()
                                                                .addGap(11)
                                                                .addComponent(wip_idText, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE))))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(33)
                                                .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
                                                .addGap(9)
                                                .addComponent(wip_yearText, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)
                                                .addGap(34)
                                                .addComponent(label_4, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(wip_upRB)
                                                .addGap(46)
                                                .addComponent(wip_downRB))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(35)
                                                .addComponent(label_5, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(ComponentPlacement.RELATED)
                                                .addComponent(wip_descriptionText, GroupLayout.PREFERRED_SIZE, 560, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(31, Short.MAX_VALUE))
                        .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
                                .addGap(156)
                                .addComponent(btnNewButton)
                                .addPreferredGap(ComponentPlacement.RELATED, 195, Short.MAX_VALUE)
                                .addComponent(btnNewButton_1)
                                .addGap(162))
        );
        gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_panel.createSequentialGroup()
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(63)
                                                .addComponent(lblNewLabel)
                                                .addGap(71)
                                                .addComponent(label_1))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(60)
                                                .addComponent(wip_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(64)
                                                .addComponent(wip_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(154)
                                                .addComponent(label_2))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(149)
                                                .addComponent(wip_amountText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(74)
                                                .addComponent(label_3))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(72)
                                                .addComponent(wip_yearText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(74)
                                                .addComponent(label_4))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(70)
                                                .addComponent(wip_upRB))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(69)
                                                .addComponent(wip_downRB)))
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(67)
                                                .addComponent(label_5))
                                        .addGroup(gl_panel.createSequentialGroup()
                                                .addGap(59)
                                                .addComponent(wip_descriptionText, GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(ComponentPlacement.RELATED, 45, Short.MAX_VALUE)
                                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                        .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
                                                .addComponent(btnNewButton_1)
                                                .addGap(25))
                                        .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
                                                .addComponent(btnNewButton)
                                                .addGap(26))))
        );
        panel.setLayout(gl_panel);

        wip_table = new JTable();
        wip_table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                tableMousePressed(me);
            }
        });
        wip_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "理财产品编号", "理财产品名称", "产品描述", "购买金额", "理财年限", "上线标志位"
                }
        ));
        wip_table.getColumnModel().getColumn(1).setPreferredWidth(86);
        wip_table.getColumnModel().getColumn(2).setPreferredWidth(151);
        wip_table.getColumnModel().getColumn(3).setPreferredWidth(108);
        wip_table.getColumnModel().getColumn(4).setPreferredWidth(86);
        wip_table.setFillsViewportHeight(true);
        scrollPane.setViewportView(wip_table);
        getContentPane().setLayout(groupLayout);

        this.fillTable(new Wip());
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ManageWip frame = new ManageWip();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 删除事件处理
     *
     * @param e
     */
    private void deleteActionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String wip_id = wip_idText.getText();
        if (StringUtils.isEmpty(wip_id)) {
            JOptionPane.showMessageDialog(null, "请选择要删除的保险产品！");
            return;
        }
        Wip wip = new Wip(StringUtils.changeInt(wip_id));
        int n = JOptionPane.showConfirmDialog(null, "确定要删除该产品吗？");
        if (n == 0) {
            int deleteNum = wipDAO.deleteWip(wip);
            if (deleteNum > 0) {
                JOptionPane.showMessageDialog(null, "删除成功！");
                this.resetValue();
                this.fillTable(new Wip());
            } else {
                JOptionPane.showMessageDialog(null, "删除失败！");
            }
        }
    }

    /**
     * 修改事件处理
     *
     * @param evt
     */
    private void updateActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String wip_id = this.wip_idText.getText();
        if (StringUtils.isEmpty(wip_id)) {
            JOptionPane.showMessageDialog(null, "请选择要修改的理财产品！");
            return;
        }

        String wip_name = this.wip_nameText.getText();
        String wip_description = this.wip_descriptionText.getText();
        String wip_amount = this.wip_amountText.getText();
        String wip_year = this.wip_yearText.getText();

        if (StringUtils.isEmpty(wip_name)) {
            JOptionPane.showMessageDialog(null, "理财产品名称不能为空！");
            return;
        }

        if (StringUtils.isEmpty(wip_description)) {
            JOptionPane.showMessageDialog(null, "理财产品介绍不能为空！");
            return;
        }

        if (StringUtils.isEmpty(wip_amount)) {
            JOptionPane.showMessageDialog(null, "理财产品价格不能为空！");
            return;
        }

        if (StringUtils.isEmpty(wip_year)) {
            JOptionPane.showMessageDialog(null, "理财产品年限不能为空！");
            return;
        }

        String wip_up = "";
        if (wip_upRB.isSelected()) {
            wip_up = "1";
        } else if (wip_downRB.isSelected()) {
            wip_up = "0";
        }

        if (StringUtils.isEmpty(wip_up)) {
            JOptionPane.showMessageDialog(null, "请选择是否上线该理财产品！");
            return;
        }

        Wip wip = new Wip(StringUtils.changeInt(wip_id), wip_name, wip_description,
                StringUtils.changeDouble(wip_amount), StringUtils.changeInt(wip_year), wip_up);
        int insertNum = wipDAO.updateWip(wip);
        if (insertNum > 0) {
            JOptionPane.showMessageDialog(null, "修改成功！");
            this.resetValue();
            this.fillTable(new Wip());
        } else {
            JOptionPane.showMessageDialog(null, "修改失败！");
        }

    }

    //重置表单
    private void resetValue() {
        // TODO Auto-generated method stub
        this.wip_idText.setText("");
        this.wip_nameText.setText("");
        this.wip_descriptionText.setText("");
        this.wip_amountText.setText("");
        this.wip_yearText.setText("");
        this.group.clearSelection();
    }

    /**
     * 表格行点击事件处理
     *
     * @param tme
     */
    private void tableMousePressed(MouseEvent tme) {
        // TODO Auto-generated method stub
        int row = this.wip_table.getSelectedRow();
        this.wip_idText.setText(String.valueOf(wip_table.getValueAt(row, 0)));
        this.wip_nameText.setText(String.valueOf(wip_table.getValueAt(row, 1)));
        this.wip_descriptionText.setText(String.valueOf(wip_table.getValueAt(row, 2)));
        this.wip_amountText.setText(String.valueOf(wip_table.getValueAt(row, 3)));
        this.wip_yearText.setText(String.valueOf(wip_table.getValueAt(row, 4)));
        String up = String.valueOf(wip_table.getValueAt(row, 5));
        if ("1".equals(up)) {
            this.wip_upRB.setSelected(true);
        } else if ("0".equals(up)) {
            this.wip_downRB.setSelected(true);
        }
    }

    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String wip_name = this.QW_nameText.getText();
        Wip wip = new Wip();
        wip.setWip_name(wip_name);
        this.fillTable(wip);
    }

    /**
     * 初始化表格
     *
     * @param wip
     */
    private void fillTable(Wip wip) {
        DefaultTableModel dtm = (DefaultTableModel) wip_table.getModel();
        dtm.setRowCount(0);//设置为0行，清空表格
        List<Wip> wipList = wipDAO.queryWip(wip);
        for (Wip rs : wipList) {
            Vector v = new Vector();
            v.add(rs.getWip_id());
            v.add(rs.getWip_name());
            v.add(rs.getWip_description());
            v.add(rs.getWip_amount());
            v.add(rs.getWip_year());
            v.add(rs.getWip_up());
            dtm.addRow(v);

        }
    }
}
